﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CollectionWall.Library;
using System.Collections.ObjectModel;
using Microsoft.Phone.Net.NetworkInformation;
using Telerik.Windows.Controls;
using Newtonsoft.Json;


namespace CollectionWall
{
    public partial class MainPage : PhoneApplicationPage
    {
        private double _dragDistanceToOpen = 80.0;
        private double _dragDistanceToClose = 200.0;
        private double _dragDistanceNegative = -80.0;
        // Constructor
        public MainPage()
        {
              InitializeComponent();
             _feContainer = this.Container as FrameworkElement;
             loaddanhmucmenu();
             loadphoto();
            
            
        }
       
		

        private FrameworkElement _feContainer;
        private bool _isSettingsOpen = false;
        private void ApplicationBarIconButton_OnClick(object sender, EventArgs e)
        {
           
        }

        private void CloseSettings()
        {

            var trans = _feContainer.GetHorizontalOffset().Transform;
            trans.Animate(trans.X, 0, TranslateTransform.XProperty, 600, 0, new CubicEase
            {
                EasingMode = EasingMode.EaseOut
            });

                //Storyboard opening = (Storyboard)FindName("Storyboard2");
                //opening.Begin();
                _isSettingsOpen = false;
            
        }

        private void OpenSettings()
        {
            
                //Storyboard opening = (Storyboard)FindName("Storyboard1");
                //opening.Begin();
            var trans = _feContainer.GetHorizontalOffset().Transform;
            trans.Animate(trans.X, 380, TranslateTransform.XProperty, 300, 0, new CubicEase
            {
                EasingMode = EasingMode.EaseOut
            });

                _isSettingsOpen = true;
            
            
        }

        private void GestureListener_OnDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
           
            if (e.Direction == System.Windows.Controls.Orientation.Horizontal && e.HorizontalChange > 0 && !_isSettingsOpen)
            {
                double offset = _feContainer.GetHorizontalOffset().Value + e.HorizontalChange;
                if (offset > _dragDistanceToOpen)
                    this.OpenSettings();
                else
                    _feContainer.SetHorizontalOffset(offset);
            }

            if (e.Direction == System.Windows.Controls.Orientation.Horizontal && e.HorizontalChange < 0 && _isSettingsOpen)
            {
                double offsetContainer = _feContainer.GetHorizontalOffset().Value + e.HorizontalChange;
                if (offsetContainer < _dragDistanceToClose)
                    this.CloseSettings();
                else
                    _feContainer.SetHorizontalOffset(offsetContainer);
            }
        }
		//su kien drag man hinh xong
        private void GestureListener_OnDragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            if (e.Direction == System.Windows.Controls.Orientation.Horizontal && e.HorizontalChange > 0 && !_isSettingsOpen)
            {
                if (e.HorizontalChange < _dragDistanceToOpen)
                    this.ResetLayoutRoot();
                else
                    this.OpenSettings();
            }

            if (e.Direction == System.Windows.Controls.Orientation.Horizontal && e.HorizontalChange < 0 && _isSettingsOpen)
            {
                if (e.HorizontalChange > _dragDistanceNegative)
                    this.ResetLayoutRoot();
                else
                   
                    this.CloseSettings();
            }
        }

		//reset lai layout sau khi tro ve
        private void SettingsStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
        {
            ResetLayoutRoot();
        }

    

        private void ResetLayoutRoot()
        {
            if (!_isSettingsOpen)
                _feContainer.SetHorizontalOffset(0.0);
            else
                _feContainer.SetHorizontalOffset(380.0);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (_isSettingsOpen)
            {
                CloseSettings();
            }
            else
            {
                OpenSettings();
            }
        }

        private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			if (_isSettingsOpen)
            {
                CloseSettings();
            }
            else
            {
                OpenSettings();
            }
        }

        private void btnSearch_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
        	
			// TODO: Add event handler implementation here.
			  if (_isSettingsOpen)
            {
                CloseSettings();
				
				
            }
            else
            {
				txtsearch.Focus();
                OpenSettings();
            }
           
			
        }




/// <summary>
/// Gọi dữ liệu thêm khi kéo xuống
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void RadDataBoundListBox_RefreshRequested(object sender, System.EventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }
        public void loaddanhmucmenu()
        {
            if(!NetworkInterface.GetIsNetworkAvailable())
            {
                this.DisplayNoConnectionInternet();
                return;
            }
            try
            {

                WebClient webClient = new WebClient();
                Uri uri = new Uri("http://myapi.somee.com/api/cate");
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompletedMenu);
                webClient.DownloadStringAsync(uri);
            }
            catch (WebException ex)
            {
                
                this.DisplayNoConnectionInternet();
                MessageBox.Show(ex.Message);
            }

        }
        void webClient_DownloadStringCompletedMenu(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                List<Models.Cate> customers = JsonConvert.DeserializeObject<List<Models.Cate>>(e.Result);
                listview01.ItemsSource = customers;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void loadphoto()
        {
            try
            {

                WebClient webClient = new WebClient();
                Uri uri = new Uri("http://myapi.somee.com/api/picture/GetPhotoByType/Type/1/Page/0");
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted2);
                webClient.DownloadStringAsync(uri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        void webClient_DownloadStringCompleted2(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                List<Models.Photo> customers = JsonConvert.DeserializeObject<List<Models.Photo>>(e.Result);
                listviewphoto.ItemsSource = customers;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (this.panel_photo.Orientation == System.Windows.Controls.Orientation.Horizontal)
            {
                this.scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                this.scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
                this.panel_photo.Orientation = System.Windows.Controls.Orientation.Vertical;
                //this.panel_photo.ItemHeight = 600;
                //this.panel_photo.ItemWidth = 460;
            }
            else if (this.panel_photo.Orientation == System.Windows.Controls.Orientation.Vertical)
            {
                this.scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                this.scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                this.panel_photo.Orientation = System.Windows.Controls.Orientation.Horizontal;
            }
        }

        private void RadImageButton_Click(object sender, RoutedEventArgs e)
        {
            loadphoto();

        }

        private void RadImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            loaddanhmucmenu();
        }

        public void DisplayNoConnectionInternet()
        {
            this.Dispatcher.BeginInvoke(()=>
                {
                          this.ErrorNotificationBlock.Text="No internet connection.Please check and retry";
                });
            

        }
        private void PhotoRequestFailed()
		{
			this.DisplayNoConnectionInternet();
		}

        /// <summary>
        /// selection item photo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listviewphoto_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //MessageBox.Show(listviewphoto.SelectedValue.ToString(),true);
        }


        }
    
    
    
    }

public class MusicItem
{
    public string SongName { get; set; }
    public string Author { get; set; }
    public string Type { get; set; }
    public string Image { get; set; }
    public string Singer { get; set; }
    public string URL { get; set; }
    public string Time { get; set; }

    public MusicItem()
    {
    }
    public MusicItem(string name, string author, string type, string singer, string url, string img, string time)
    {
        this.SongName = name;
        this.Singer = singer;
        this.Image = img;
        this.URL = url;
        this.Time = time;
        this.Author = author;
        this.Type = type;

    }


}
